package com.tanhua.recommend.msg;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.recommend.pojo.RecommendQuanZi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;

/**
 * @Author: ymm
 * @Description: 类内容说明
 * @Date: Created in 20:13 2021/1/11
 * @ModifiedBy:
 */
@Component
@RocketMQMessageListener(topic = "tanhua-quanzi",consumerGroup = "tanhua-quanzi-consumer")
@Slf4j
public class QuanZiMsgConsumer implements RocketMQListener<String> {
    @Autowired
    private MongoTemplate mongoTemplate;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Override
    public void onMessage(String msg) {
        try {
            JsonNode jsonNode = MAPPER.readTree(msg);

            long userId = jsonNode.get("userId").asLong();
            long pid = jsonNode.get("pid").asLong();
            String publishId = jsonNode.get("publishId").asText();
            Integer type = jsonNode.get("type").asInt();

            //1-发动态，2-浏览动态， 3-点赞， 4-喜欢， 5-评论，6-取消点赞，7-取消喜欢
            RecommendQuanZi recommendQuanZi = new RecommendQuanZi();
            recommendQuanZi.setUserId(userId);
            recommendQuanZi.setId(ObjectId.get());
            recommendQuanZi.setDate(System.currentTimeMillis());
            recommendQuanZi.setPublishId(pid);

            /**
             * - 浏览 +1
             - 点赞 +5
             - 喜欢 +8
             - 评论 + 10
             - 文字长度：50以内1分，50~100之间2分，100以上3分
             - 图片个数：每个图片一分
             */
            switch (type){
                case 1:{
                    int score = 0;
                    Publish publish = this.mongoTemplate.findById(new ObjectId(publishId), Publish.class);
                    if (StringUtils.length(publish.getText()) < 50){
                        score += 1;
                    }else if (StringUtils.length(publish.getText()) < 100){
                        score += 2;
                    }else if(StringUtils.length(publish.getText()) >= 100){
                        score += 3;
                    }

                    if (!CollectionUtils.isEmpty(publish.getMedias())){
                        score += publish.getMedias().size();
                    }

                    recommendQuanZi.setScore((Double.valueOf(score)));
                    break;
                }case 2: {
                    recommendQuanZi.setScore(1d);
                    break;
                }
                case 3: {
                    recommendQuanZi.setScore(5d);
                    break;
                }
                case 4: {
                    recommendQuanZi.setScore(8d);
                    break;
                }
                case 5: {
                    recommendQuanZi.setScore(10d);
                    break;
                }
                case 6: {
                    recommendQuanZi.setScore(-5d);
                    break;
                }
                case 7: {
                    recommendQuanZi.setScore(-8d);
                    break;
                }
                default: {
                    recommendQuanZi.setScore(0d);
                    break;
                }
            }


            String collectionName = "recommend_quanzi_" + new DateTime().toString("yyyyMMdd");
            this.mongoTemplate.save(recommendQuanZi,collectionName);
        } catch (IOException e) {
            log.error("处理消息失败~" + msg, e);
        }
    }
}
